package cn.xspace.module.other.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xspace.android.xafcommon.util.DateUtils;
import com.xspace.android.xafcommon.util.LogUtil;

import java.util.List;

import cn.xspace.module.other.R;
import cn.xspace.module.other.bean.FavoriteArticalBean;

/**
 * Wubj 创建于 2017/1/4 0004.
 */
public class MyFavoriteAdapter extends BaseAdapter {
    private static final String TAG = MyFavoriteAdapter.class.getSimpleName();

    private Context mContext;
    private List<FavoriteArticalBean> dataList;

    public MyFavoriteAdapter(Context context, List<FavoriteArticalBean> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public Object getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LogUtil.d(TAG, "wubaojie>>>getView: position :" + position);
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.list_item_myfavorite, null);
            holder = new ViewHolder(convertView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final FavoriteArticalBean fab = dataList.get(position);
//        String datetimeDesc = DateUtils.getDatetimeDesc(fab.time, DateUtils.FORMAT_1);
//        String datetimeDesc = DateUtils.getChatDatetimeDesc(fab.time);
        String datetimeDesc = DateUtils.getMessageList(fab.time,DateUtils.FORMAT_1);
        holder.tv_date.setText(datetimeDesc); // TODO 设置时间和来源
        holder.tv_from.setText(String.format("来自%s", fab.msg_org));
        holder.tv_file_title.setText(fab.file_title);
//        holder.tv_content.setText(fab.file_title);
//        if ("8".equals(fab.msg_type)) {
//            holder.iv_image.setVisibility(View.GONE);
//        } else if ("5".equals(fab.msg_type)) {
//            holder.iv_image.setVisibility(View.VISIBLE);
//            Picasso.with(mContext)
//                        .load(StringUtils.getUri(fab.image_txt_url))
//                        .placeholder(R.drawable.chat_default_image)
//                        .error(R.drawable.chat_default_image_error)
//                        .into(holder.iv_image);
//        }

//        if ("0".equals(fab.msg_type)) { //文章
//            holder.image.setVisibility(View.GONE);
//            holder.tv_msg_title.setVisibility(View.GONE);
//            holder.fl_chat_vi.setVisibility(View.GONE);
//            holder.rel_image_text.setVisibility(View.GONE);
//            holder.tv_content.setText(fab.getContent_title());
//        } else {
//            holder.tv_content.setText((!StringUtils.isNull(fab.getContent_title()) ? fab.getContent_title() : fab
//                    .sender_name));
//            if ("1".equals(fab.msg_type)) {//图片
//                holder.rel_image_text.setVisibility(View.GONE);
//                holder.image.setVisibility(View.VISIBLE);
//                holder.tv_msg_title.setVisibility(View.GONE);
//                holder.fl_chat_vi.setVisibility(View.GONE);
//                Picasso.with(mContext)
//                        .load(StringUtils.getUri(fab.file_url))
//                        .placeholder(R.drawable.chat_default_image)
//                        .error(R.drawable.chat_default_image_error)
//                        .into(holder.image);
//            } else if ("2".equals(fab.msg_type)) {
//                holder.rel_image_text.setVisibility(View.GONE);
//                holder.image.setVisibility(View.GONE);
//                holder.tv_msg_title.setVisibility(View.GONE);
//                holder.fl_chat_vi.setVisibility(View.VISIBLE);
//                holder.audio_img.setImageResource(RProxy.active_news_icon);
//                String times = fab.file_url.substring(fab.file_url.lastIndexOf("/") + 1);
//                holder.tv_audio_text.setText("语音 " + times.substring(0, times.indexOf("-")) + "\"");
//                //holder.tv_audio_time.setText(times.substring(0, times.indexOf("-")) + "\"");
//            } else if ("4".equals(fab.msg_type)) {//文件
//                holder.image.setVisibility(View.GONE);
//                holder.tv_msg_title.setVisibility(View.GONE);
//                holder.rel_image_text.setVisibility(View.GONE);
//                holder.fl_chat_vi.setVisibility(View.VISIBLE);
//                try {
//                    JSONObject json = new JSONObject(fab.file_url);
//                    String filename = json.optString("fileName");
//                    String fileType = json.optString("fileType");
//                    if (TextUtils.isEmpty(fileType) && filename.indexOf(".") != -1) {
//                        fileType = filename.substring(filename.indexOf(".") + 1, filename.length());
//                    }
//                    if (filename.indexOf(".") == -1) {
//                        filename += "." + fileType;
//                    }
//                    //long fileSize = json.optLong("size");
//                    String uploadUrl = json.optString("coneten");
//                    holder.tv_audio_text.setText(filename);
//                    //holder.tv_audio_time.setText(FileMg.FormetFileSize(fileSize));
//                    setFileTypeImg(holder.audio_img, fileType, uploadUrl);
//                } catch (JSONException e) {
//                    e.printStackTrace();
//                }
//            } else if ("5".equals(fab.msg_type)) {//图文
//                holder.image.setVisibility(View.GONE);
//                holder.tv_msg_title.setVisibility(View.GONE);
//                holder.rel_image_text.setVisibility(View.GONE);
//                holder.fl_chat_vi.setVisibility(View.GONE);
//                holder.rel_image_text.setVisibility(View.VISIBLE);
//                holder.setImgtext(fab.file_url);
//                fab.image_txt_url = holder.src;
//            } else {
//                holder.tv_msg_title.setText(fab.file_url);
//                holder.image.setVisibility(View.GONE);
//                holder.rel_image_text.setVisibility(View.GONE);
//                holder.fl_chat_vi.setVisibility(View.GONE);
//                holder.tv_msg_title.setVisibility(View.VISIBLE);
//            }
//        }
        return convertView;
    }

    class ViewHolder {
        TextView tv_file_title;
        //        TextView tv_content;
        TextView tv_date;
        TextView tv_from;

        public ViewHolder(View view) {
            tv_file_title = (TextView) view.findViewById(R.id.tv_fav_title);
//            tv_content = (TextView) view.findViewById(R.id.tv_fav_content);
            tv_from = (TextView) view.findViewById(R.id.tv_fav_from);
            tv_date = (TextView) view.findViewById(R.id.tv_fav_date);
        }
    }

//    class ViewHolder {
//        TextView tv_date;
//        TextView tv_content;
//        ImageView image, audio_img, img_imgtxt_icon;
//        RelativeLayout fl_chat_vi, rel_image_text;
//        TextView tv_audio_time, tv_audio_text;
//        TextView tv_msg_title;
//        TextView tv_imgtxt_title, tv_imgtxt_content;
//        ImageView img;
//        String src;
//
//        public ViewHolder(View view) {
//            tv_date = (TextView) view.findViewById(R.id.tv_myfavorite_web_time);
//            tv_content = (TextView) view.findViewById(R.id.tv_myfavorite_web_content);
//            image = (ImageView) view.findViewById(R.id.image);
//            fl_chat_vi = (RelativeLayout) view.findViewById(R.id.fl_chat_vi);
//            tv_audio_time = (TextView) view.findViewById(R.id.tv_audio_time);
//            tv_audio_text = (TextView) view.findViewById(R.id.tv_audio_title);
//            tv_msg_title = (TextView) view.findViewById(R.id.tv_msg_title);
//            audio_img = (ImageView) view.findViewById(R.id.iv_chat_vi);
//            img = (ImageView) view.findViewById(R.id.img_favorite_cancel_web);
//
//            rel_image_text = (RelativeLayout) view.findViewById(R.id.rel_circle_notice_content);
//            tv_imgtxt_title = (TextView) view.findViewById(R.id.txt_image_title);
//            img_imgtxt_icon = (ImageView) view.findViewById(R.id.txt_image_icon);
//            tv_imgtxt_content = (TextView) view.findViewById(R.id.txt_image_content);
//        }
//
//        private void setImgtext(String content0) {
//            JSONObject jb;
//            try {
//                jb = new JSONObject(content0);
//                JSONArray jb1 = jb.getJSONArray("newssnap");
//                JSONObject jb2 = jb1.getJSONObject(0);
//                String content = jb2.optString("summary");
//                String title = jb2.getString("title");
//                String img = jb2.getString("img");
//                src = jb2.getString("src");
//                tv_imgtxt_content.setText(content);
//                tv_imgtxt_title.setText(title);
//                if (!TextUtils.isEmpty(img)) {
//                    ImageUtils.showImage(img,
//                            img_imgtxt_icon, null);
//                }
//            } catch (JSONException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//
//        }
//    }

//    private void setFileTypeImg(ImageView iv_chat_pic, String fileType, String uploadUrl) {
//        if ("mp3".equals(fileType) || "wma".equals(fileType) || "amr".equals(fileType)) {
//            iv_chat_pic.setImageResource(RProxy.file_icon_mp3);
//            iv_chat_pic.setTag("audio/*");
//        } else if ("mp4".equals(fileType) || "rmvb".equals(fileType)) {
//            iv_chat_pic.setImageResource(RProxy.file_icon_video);
//            iv_chat_pic.setTag("video/*");
//        } else if ("doc".equals(fileType) || "docx".equals(fileType) || "wps".equals(fileType) ||
//                "xls".equals(fileType)) {
//            iv_chat_pic.setImageResource(RProxy.file_icon_office);
//            iv_chat_pic.setTag("application/*");
//        } else if ("pdf".equals(fileType)) {
//            iv_chat_pic.setImageResource(RProxy.file_icon_pdf);
//            iv_chat_pic.setTag("application/pdf");
//        } else if ("jpg".equals(fileType) || "png".equals(fileType)) {
//            String imgUrl = uploadUrl;
//            if (!TextUtils.isEmpty(imgUrl)) {
//                Picasso.with(mContext)
//                        .load(StringUtils.getUri(imgUrl))
//                        .placeholder(R.drawable.chat_default_image)
//                        .error(R.drawable.chat_default_image_error)
//                        .into(iv_chat_pic);
//            } else {
//                iv_chat_pic.setImageResource(RProxy.file_icon_image);
//            }
//            iv_chat_pic.setTag("image/*");
//        } else if ("jar".equals(fileType) || "rar".equals(fileType)) {
//            iv_chat_pic.setImageResource(RProxy.file_icon_rar);
//            iv_chat_pic.setTag("application/*");
//        } else if ("zip".equals(fileType)) {
//            iv_chat_pic.setImageResource(RProxy.file_icon_zip);
//            iv_chat_pic.setTag("application/*");
//        } else if ("txt".equals(fileType) || "ebk3".equals(fileType)) {
//            iv_chat_pic.setImageResource(RProxy.file_icon_txt);
//            iv_chat_pic.setTag("application/*");
//        } else if ("apk".equals(fileType)) {
//            iv_chat_pic.setImageResource(RProxy.file_icon_apk);
//            iv_chat_pic.setTag("application/vnd.android.package-archive");
//        } else {
//            iv_chat_pic.setImageResource(RProxy.file_icon_default);
//            iv_chat_pic.setTag("application/*");
//        }
//    }
}
